package leetcode;

/**
 * @Author: Liu Yue
 * @Descripition: 整数反转
 * @Date; Create in 2020/2/16 14:19
 **/
public class Zhengshufanzhuan {

    //官方解法
    static int reverse(int x){
        int rev = 0;


        System.out.println("int 的最大数：" + Integer.MAX_VALUE);
        System.out.println("int 的最小数：" + Integer.MIN_VALUE);
        while( x != 0){

            int pop = x % 10;

            x  /= 10;

            if(rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > Integer.MAX_VALUE % 10)){
                rev = 0;
                break;
            }else if(rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && x < Integer.MIN_VALUE % 10)){
                rev = 0;
                break;
            }
            rev = rev * 10 + pop;

        }
        return  rev;
    }
    //强人解法
    static int reverse2(int x) {
        int ans = 0;
        while (x != 0) {
            if ((ans * 10) / 10 != ans) {
                ans = 0;
                break;
            }
            ans = ans * 10 + x % 10;
            x /= 10;
        }
        return ans;
    }
    public static void main(String[] args){
        //System.out.println(Zhengshufanzhuan.reverse(12345678));
        System.out.println(Zhengshufanzhuan.reverse2(12345678));
    }
}
